package cn.lili.controller.promotion;

import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.Seckill;
import cn.lili.modules.promotion.entity.dos.SeckillApply;
import cn.lili.modules.promotion.entity.dto.SeckillAutidDTO;
import cn.lili.modules.promotion.entity.dto.search.SeckillSearchParams;
import cn.lili.modules.promotion.entity.vos.SeckillApplyVO;
import cn.lili.modules.promotion.entity.vos.SeckillGoodsVO;
import cn.lili.modules.promotion.entity.vos.SeckillTimelineVO;
import cn.lili.modules.promotion.entity.vos.SeckillVO;
import cn.lili.modules.promotion.service.SeckillApplyService;
import cn.lili.modules.promotion.service.SeckillService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 秒杀活动接口
 *
 * @author paulG
 * @since 2020/8/20
 **/
@RestController
@Api(tags = "秒杀活动接口")
@RequestMapping("/promotion/seckill")
@RequiredArgsConstructor
public class SeckillController {

    private final SeckillService seckillService;

    private final SeckillApplyService seckillApplyService;


    @ApiOperation(value = "初始化秒杀活动(初始化方法，默认初始化30天内的活动）")
    @GetMapping("/init")
    public ResultMessage addSeckill() {
        seckillService.generatorSeckill();
        return ResultUtil.success();
    }


    @ApiOperation(value = "修改秒杀活动")
    @PutMapping
    public ResultMessage<Seckill> updateSeckill(@RequestBody SeckillVO seckillVO) {
        seckillService.updatePromotions(seckillVO);
        return ResultUtil.data(seckillVO);
    }

    @ApiOperation(value = "通过id获取")
    @ApiImplicitParam(name = "id", value = "秒杀活动ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResultMessage<Seckill> get(@PathVariable String id) {
        Seckill seckill = seckillService.getSeckillById(id);
        return ResultUtil.data(seckill);
    }

    @ApiOperation(value = "分页查询秒杀活动列表")
    @GetMapping
    public ResultMessage<Page<Seckill>> getAll(SeckillSearchParams param, PageVO pageVo) {
        return ResultUtil.data(seckillService.pageFindAll(param, pageVo));
    }

    @ApiOperation(value = "删除一个秒杀活动")
    @ApiImplicitParam(name = "id", value = "秒杀活动ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultMessage<Object> deleteSeckill(@PathVariable String id) {
        seckillService.removePromotions(Collections.singletonList(id));
        return ResultUtil.success();
    }

    @ApiOperation(value = "操作秒杀活动状态")
    @ApiImplicitParam(name = "id", value = "秒杀活动ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/status/{id}")
    public ResultMessage<Object> updateSeckillStatus(@PathVariable String id, Long startTime, Long endTime) {
        seckillService.updateStatus(Collections.singletonList(id), startTime, endTime);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取秒杀活动申请列表")
    @GetMapping("/apply")
    public ResultMessage<Page<SeckillApply>> getSeckillApply(SeckillSearchParams param, PageVO pageVo) {
        String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getExtendId();
        if(!"-1".equals(storeId)) {
            param.setStoreIds(new String[]{storeId});
        }
        Page<SeckillApply> seckillApply = seckillApplyService.getSeckillApplyPage(param, pageVo);
        return ResultUtil.data(seckillApply);
    }

    @PostMapping(path = "/apply/{seckillId}")
    @ApiOperation(value = "添加秒杀活动申请")
    public ResultMessage<String> addSeckillApply(@PathVariable String seckillId, @RequestBody List<SeckillApplyVO> applyVos) {
        String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getExtendId();
        seckillApplyService.addSeckillApply(seckillId, storeId, applyVos);
        return ResultUtil.success();
    }

    @DeleteMapping("/apply/{seckillId}/{id}")
    @ApiOperation(value = "删除秒杀活动申请")
    public ResultMessage<String> deleteSeckillApply(@PathVariable String seckillId, @PathVariable String id) {
        String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getExtendId();
        seckillApplyService.removeSeckillApply(seckillId, storeId, id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取当天秒杀活动信息")
    @GetMapping("/today")
    public ResultMessage<List<SeckillTimelineVO>> getSeckillTime() {
        return ResultUtil.data(seckillApplyService.getSeckillTimeline());
    }

    @ApiOperation(value = "获取某个时刻的秒杀活动商品信息")
    @GetMapping("/time/{timeline}")
    public ResultMessage<List<SeckillGoodsVO>> getSeckillGoods(@PathVariable Integer timeline) {
        return ResultUtil.data(seckillApplyService.getSeckillGoods(timeline));
    }

    @ApiOperation(value = "获取某个时刻的秒杀活动商品信息")
    @PutMapping("/apply/audit")
    public ResultMessage auditSeckillApply(@RequestBody SeckillAutidDTO seckillAutidDTO) {
        seckillApplyService.auditSeckillApply(seckillAutidDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据goodsId获取秒杀商品sku")
    @GetMapping("/getSeckillGoodsSkuList/{seckillId}/{goodsId}")
    public ResultMessage<List<SeckillGoodsVO>> getSeckillGoodsSkuList (@PathVariable String seckillId, @PathVariable String goodsId) {
        return ResultUtil.data(seckillApplyService.getSeckillGoodsSkuList(seckillId, goodsId));
    }

}
